package ui;

import java.awt.Dimension;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;

import ui.grafic.Grafics;
import ui.model.TableListModel;
import databaseConnect.HostConfigurationRequestProcessor;
import databaseConnect.HostConfigurationUpdateOrInsertProcessor;
import databaseConnect.ScenarioConfigurationRequestProcessor;

public class ScenarioOriginatorHostComponentSplitPane extends JSplitPane {
	// Variables
	
	private Grafics grafikPane = null;
	private JFrame grafikPaneContainer = null;
	private int grafikPaneCorrectWidth = 16;
	private int grafikPaneCorrectHeight = 36;
	
	private ScenarioOriginatorIFrame so = null;
	
	private ScenarioOriginiatorHostComponentHostSelectionListContainer hsl = new ScenarioOriginiatorHostComponentHostSelectionListContainer(this);
	
	//in this Scenario selected Hosts
	private JTable HostList = null;
	// Methods
	public ScenarioOriginatorHostComponentSplitPane(ScenarioOriginatorIFrame source) {
		super();
		so = source;
		JScrollPane myL = new JScrollPane(this.getHostListFromDB(so.getScenarioID()));
		this.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
		this.setLeftComponent(myL);
		this.setRightComponent(hsl);
		this.setDividerLocation(200);
		this.getGrafikPaneContainer();
		this.addExistingHosts();
	}


	private void addExistingHosts() {
		// TODO Auto-generated method stub
		Vector<Vector<String>> tmp = ScenarioConfigurationRequestProcessor.getHostRowsForGrafik(so.getScenarioID());
		for (Vector<String> tmptmp : tmp){
			getGrafikPane().addPoint(Integer.parseInt(tmptmp.get(0)), Integer.parseInt(tmptmp.get(1)), tmptmp.get(2));
		}
	}


	private JTable getHostListFromDB(int scenarioID) {
		if (HostList == null) {
			Vector<String> tmp = new Vector<String>();
			tmp.add("Selected Hosts");
			Vector<Vector<String>>tmptmp = ScenarioConfigurationRequestProcessor.getHostRows(scenarioID);
			for (Vector<String> blub : tmptmp){
				blub.set(0, "Host " + blub.get(0));
			}
			HostList = new JTable(new TableListModel(tmptmp, tmp));
		}
		return HostList;
	}

	public JTable getHostList() {
		return HostList;
	}


	public void addHost() {
		if (hsl.getHostSelectionList().getSelectedRow() < 0 && hsl.getHostSelectionList().getRowCount() > 0) {
			hsl.getHostSelectionList().changeSelection(0, 0, false, false);

		} 
		if (hsl.getHostSelectionList().getRowCount() == 0) {
			// nothing
		} else {
			Vector <String> tmp = new Vector<String>();
			tmp.add("Host " + hsl.getHostIdFromSelectedRow(hsl.getHostSelectionList().getSelectedRow()));
			((TableListModel) HostList.getModel()).addRow(tmp);
			getGrafikPane().addPoint(hsl.getHostPosXFromSelectedRow(hsl.getHostSelectionList().getSelectedRow()), hsl.getHostPosYFromSelectedRow(hsl.getHostSelectionList().getSelectedRow()), hsl.getHostIdFromSelectedRow(hsl.getHostSelectionList().getSelectedRow()));
			HostList.updateUI();
			getGrafikPane().repaint();
		}		
	}

	public void deleteHost() {
		if (HostList.getSelectedRow() < 0 && HostList.getRowCount() > 0 || HostList.getRowCount() <= HostList.getSelectedRow()) {
			HostList.changeSelection(0, 0, false, false);
		} 
		if (HostList.getRowCount() == 0) {
			// nothing
		} else {
			System.out.println(hsl.getHostSelectionList().getSelectedRow());
			getGrafikPane().removePoint(hsl.getHostPosXFromSelectedRow(hsl.getHostSelectionList().getSelectedRow())+so.getMiniworldRange(), hsl.getHostPosYFromSelectedRow(hsl.getHostSelectionList().getSelectedRow())+so.getMiniworldRange());
			((TableListModel) HostList.getModel()).deleteRow(HostList
					.getSelectedRow());
			HostList.updateUI();
			getGrafikPane().repaint();
		}
	}

	public Vector<Vector<String>> getHosts() {
		if (((TableListModel) HostList.getModel()).getAllValues().size() > 0) {
			return ((TableListModel) HostList.getModel()).getAllValues();
		}
		return new Vector<Vector<String>>();
	}

	
	protected JFrame getGrafikPaneContainer() {
		if (grafikPaneContainer == null) {
			this.grafikPaneContainer = new JFrame();
			this.grafikPaneContainer.setTitle(so.getScenarioID()+"");
			this.grafikPaneContainer.setVisible(true);
			this.grafikPaneContainer.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
			this.grafikPaneContainer.setSize(new Dimension((int)getGrafikPane().getSize().getWidth()+grafikPaneCorrectWidth, (int)getGrafikPane().getSize().getHeight()+grafikPaneCorrectHeight));
			this.grafikPaneContainer.add(this.getGrafikPane());
	}
		return grafikPaneContainer;
	}
	protected Grafics getGrafikPane() {
		if (grafikPane == null) {
			grafikPane = new Grafics(so.getMiniworldWidth(), so.getMiniworldHeight(), so.getMiniworldRange());
		}
		return grafikPane;
	}


	protected void updateGraphic(){
		getGrafikPane().updateGrafic(so.getMiniworldWidth(),so.getMiniworldHeight(), so.getMiniworldRange());
		getGrafikPaneContainer().setSize(new Dimension((int) getGrafikPane().getSize().getWidth()+grafikPaneCorrectWidth, (int) getGrafikPane().getSize().getHeight()+grafikPaneCorrectHeight));
	}
	
	protected int getMiniworldWidth(){
		return so.getMiniworldWidth();
	}
	
	protected int getMiniworldHeight(){
		return so.getMiniworldHeight();
	}
}